﻿using Inaction.Criteria;
using System;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Inaction.Business.Security
{
    [Serializable]
    public class MenuCriteria : Inaction.Criteria.CriteriaBase
    {
        [CriteriaField("AppType", Logic = CriteriaLogic.And, Operate = CriteriaOperate.Equal)]
        private AppType appType = AppType.WinForm;
        [Display(Description = "应用类型"), DisplayName("应用类型")]
        public AppType AppType
        {
            get { return appType; }
            set { appType = value; }
        }

        [CriteriaField("ParentId", Logic = CriteriaLogic.And, Operate = CriteriaOperate.IsNull)]
        private bool? _parentIdIsNull;
        [Display(Description = "上级菜单"), DisplayName("上级菜单")]
        public bool? ParentIdIsNull
        {
            get { return _parentIdIsNull; }
            set { _parentIdIsNull = value; }
        }

        [CriteriaField("Id", Logic = CriteriaLogic.And, Operate = CriteriaOperate.In)]
        private string _idIn;
        [Display(Description = "菜单角色"), DisplayName("菜单角色")]
        public string[] IdInRoleCode
        {
            get
            {
                return _idIn == null ? null : _idIn.Substring(_idIn.IndexOf('(')).Replace("(", "").Replace(")", "").Split(',').Select(p => p.Substring(1, p.Length - 1)).ToArray();
            }
            set
            {
                if (value == null)
                {
                    _idIn = string.Empty;
                    return;
                }
                var classInfo = MenuRoleV.ClassInfo;
                var sql = "Select " + MenuRoleV.MenuIdProperty.Column.FormatName + " From " + classInfo.Table.FormatName
                    + " Where " + MenuRoleV.CodeProperty.Column.FormatName + " In('" + string.Join("','", value) + "')";
                _idIn = sql;
            }
        }
    }
}
